/// 分配内存
#[inline(always)]
pub fn new<T>() -> *mut T
{
    unsafe { std::alloc::alloc(std::alloc::Layout::new::<T>()) as *mut T }
}

/// 分配内存，并且填充0
#[inline(always)]
pub fn new_zeroed<T>() -> *mut T
{
    unsafe { std::alloc::alloc_zeroed(std::alloc::Layout::new::<T>()) as *mut T }
}

/// 释放内存，并且调用drop()
#[inline(always)]
pub fn delete<T>(ptr: *mut T)
{
    unsafe {
        std::ptr::drop_in_place(ptr);
        std::alloc::dealloc(ptr as *mut u8, std::alloc::Layout::new::<T>())
    };
}

/// 分配多个连续内存
#[inline(always)]
pub fn news<T>(count: usize) -> *mut T
{
    unsafe { std::alloc::alloc(std::alloc::Layout::from_size_align_unchecked(std::mem::size_of::<T>() * count, std::mem::align_of::<T>())) as *mut T }
}

/// 分配多个连续内存，并且填充0
#[inline(always)]
pub fn news_zeroed<T>(count: usize) -> *mut T
{
    unsafe { std::alloc::alloc_zeroed(std::alloc::Layout::from_size_align_unchecked(std::mem::size_of::<T>() * count, std::mem::align_of::<T>())) as *mut T }
}

/// 释放多个连续内存，并且每个调用drop()
#[inline(always)]
pub fn deletes<T>(ptr: *mut T, count: usize)
{
    unsafe {
        if std::mem::needs_drop::<T>()
        {
            for i in 0..count
            {
                std::ptr::drop_in_place(ptr.offset(i as isize));
            }
        }
        std::alloc::dealloc(
            ptr as *mut u8,
            std::alloc::Layout::from_size_align_unchecked(std::mem::size_of::<T>() * count, std::mem::align_of::<T>()),
        );
    }
}
